/// @tags:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 1e5 + 5;
ll const INF = 0x0f3f3f3f3f3f3f3f;

struct Node {
  int st, edl, edr, pos;
  ll val, tmp;
  Node(int st, int edl, int edr, int pos, ll val, ll tmp)
      : st(st), edl(edl), edr(edr), pos(pos), val(val), tmp(tmp) {}
  inline bool operator<(Node const &rhs) const {
    return val == rhs.val ? (st == rhs.st ? pos < rhs.pos : st < rhs.st) : val < rhs.val;
  }
};

struct Query {
  int pos;
  ll val;
  Query(int pos = 0, ll val = 0) : pos(pos), val(val) {}
  inline Query operator+(Query const &rhs) const {
    Query res(val == rhs.val ? (pos < rhs.pos ? *this : rhs)
                             : (val > rhs.val ? *this : rhs));
    return res;
  }
};

struct SegNode : Query {
  int ls, rs;
  ll tag;

  inline void operator=(Query const &rhs) { pos = rhs.pos, val = rhs.val; }

  inline void operator=(SegNode const &rhs) {
    pos = rhs.pos, val = rhs.val, tag = rhs.tag;
  }

  inline Query operator+(SegNode const &rhs) const {
    SegNode res(val == rhs.val ? (pos < rhs.pos ? *this : rhs)
                               : (val > rhs.val ? *this : rhs));
    return res;
  }
} tr[N * 30];

int a[N], pre[N], cnt, rt[N], ed[N];
vector<pair<int, int> > vec;
map<int, int> mp;
priority_queue<Node> pq;

void modify(int l, int r, int &x, int pre, int L, int R, int c) {
  if (!x) x = ++cnt;
  tr[x] = tr[pre];
  if (L <= l && r <= R) {
    tr[x].val += c;
    tr[x].tag += c;
    tr[x].ls = tr[pre].ls, tr[x].rs = tr[pre].rs;
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) modify(l, mid, tr[x].ls, tr[pre].ls, L, R, c);
  else
    tr[x].ls = tr[pre].ls;
  if (mid < R) modify(mid + 1, r, tr[x].rs, tr[pre].rs, L, R, c);
  else
    tr[x].rs = tr[pre].rs;
  tr[x] = tr[tr[x].ls] + tr[tr[x].rs];
  tr[x].val += tr[x].tag;
}

Query query(int l, int r, int x, int L, int R) {
  if (L > R) return Query(0, -INF);
  if (L <= l && r <= R) return tr[x];
  int mid = (l + r) >> 1;
  Query res(0, -INF);
  if (L <= mid) res = res + query(l, mid, tr[x].ls, L, R);
  if (mid < R) res = res + query(mid + 1, r, tr[x].rs, L, R);
  res.val += tr[x].tag;
  return res;
}

void build(int l, int r, int &x) {
  x = ++cnt;
  tr[x].pos = l;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(l, mid, tr[x].ls), build(mid + 1, r, tr[x].rs);
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], pre[i] = mp[a[i]], mp[a[i]] = i,
                 vec.push_back(pair<int, int>(pre[i], i));
  sort(vec.begin(), vec.end());
  build(1, n, rt[0]);
  for (int i = 0; i < n; ++i) {
    modify(1, n, rt[i + 1], rt[i], vec[i].second, n, a[vec[i].second]);
    ed[vec[i].first] = i + 1;
  }
  for (int i = 1; i <= n; ++i)
    if (!ed[i]) ed[i] = ed[i - 1];
  for (int i = 1; i <= n; ++i) {
    Query t = query(1, n, rt[ed[i - 1]], i, n),
          t2 = (i == 1) ? Query(0, 0) : query(1, n, rt[ed[i - 1]], i - 1, i - 1);
    pq.push(Node(i, i, n, t.pos, t.val - t2.val, t2.val));
  }
  while (--k) {
    Node t = pq.top();
    pq.pop();
    if (t.edl < t.pos) {
      Query tmp = query(1, n, rt[ed[t.st - 1]], t.edl, t.pos - 1);
      pq.push(Node(t.st, t.edl, t.pos - 1, tmp.pos, tmp.val - t.tmp, t.tmp));
    }
    if (t.pos < t.edr) {
      Query tmp = query(1, n, rt[ed[t.st - 1]], t.pos + 1, t.edr);
      pq.push(Node(t.st, t.pos + 1, t.edr, tmp.pos, tmp.val - t.tmp, t.tmp));
    }
  }
  cout << pq.top().val;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("T2.in", "r", stdin);
  freopen("T2.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
